package cn.com.czpb.client.app;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;

import com.gyf.barlibrary.ImmersionBar;
import cn.com.czpb.client.custorm.CustomProgressDialog;
import cn.com.czpb.client.custorm.CustomProgressInDexDialog;
import cn.com.czpb.client.utils.SPUtil;

import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by 猪猪侠 on 2017/6/30.
 */

public abstract class BaseFragment extends Fragment implements View.OnTouchListener {

    private Unbinder unbinder;
    public Activity activity;
    protected FragmentManager fm;
    protected SPUtil spu;//
    public View root;
    public CustomProgressInDexDialog dialog_index;
    public CustomProgressDialog dialog;
    protected ImmersionBar mImmersionBar;//沉浸式布局

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        activity = getActivity();
        fm=getChildFragmentManager();
        spu=SPUtil.getInstance();
    }
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
       root = inflater.inflate(setMyContentView(),null);

        unbinder = ButterKnife.bind(this,root);
        initImmersionBar();

        initView();
        initData();
        return root;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    public  abstract int setMyContentView();
    public abstract void initView();
    public abstract void initData();


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
        if (mImmersionBar != null)
            mImmersionBar.destroy();
    }
    /**
     * 初始化沉浸式
     */
    protected void initImmersionBar() {
        mImmersionBar = ImmersionBar.with(this);
        mImmersionBar.keyboardEnable(true)
                .statusBarDarkFont(true)
                .navigationBarWithKitkatEnable(false).init();
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        // 拦截触摸事件，防止泄露下去
        view.setOnTouchListener(this);
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        return true;
    }
    public void showDialog_index() {
//        if (dialog_index == null)
//            dialog_index = CustomProgressInDexDialog.createDialog(activity, false);
//        dialog_index.show();
    }

    public void disMissDialog_index() {
//        if (dialog_index != null) {
//            if (dialog_index.isShowing()) {
////                dialog_index.dismiss();
//            }
//        }
    }
    public void showDialog() {
//        if (dialog == null)
//            dialog = CustomProgressDialog.createDialog(activity, false);
//        dialog.show();
    }

    public void disMissDialog() {
//        if (dialog != null) {
//            if (dialog.isShowing()) {
//                dialog.dismiss();
//            }
//        }
    }

//    @Override
//    public void onHiddenChanged(boolean hidden) {
//        super.onHiddenChanged(hidden);
//        if (!hidden && mImmersionBar != null)
//            mImmersionBar.init();
//    }
}
